package com.ubo.main;

import javafx.scene.Group;

import com.ubo.graphviz.model.graph.EGraphType;
import com.ubo.transform.dot.DotGenerator;
import com.ubo.transform.java.GraphvizGraphBuilder;
import com.ubo.transform.java.IJavaVisitor;
import com.ubo.transform.java.JavaGenerator;
import com.ubo.transform.java.JavaGraphvizTransformer;
import com.ubo.uspeed.SchemaUML;
import com.ubo.uspeed.Visiteur;
import com.ubo.uspeed.analyser.USpeedParser;
import com.ubo.visu.parser.DotParser;
import com.ubo.xml.analyser.DTDParser;

/**
 * 
 * @author Guillaume
 * 
 */
public class MainGenerator {

	protected Visiteur mVisiteur;

	protected IJavaVisitor<StringBuffer> mJavaGenerator;

	public String generateXML(String string) {
		mVisiteur = new Visiteur();
		USpeedParser u = new USpeedParser();
		u.generateSchemaUml(string);
		u.getsU().accept(mVisiteur);
		return mVisiteur.getOuput();
	}

	public String generateJavaFromUSpeed(String string) {
		mVisiteur = new Visiteur();
		USpeedParser u = new USpeedParser();
		u.generateSchemaUml(string);
		u.getsU().accept(mVisiteur);
		mVisiteur.generateXML("schema.xml");
		DTDParser parser = new DTDParser("schema.xml");
		parser.generateJavaModel();
		mJavaGenerator = new JavaGenerator();
		parser.getJm().generate(mJavaGenerator);
		return mJavaGenerator.getValue().toString();
	}

	public String generateJavaFromFileXML() {
		DTDParser parser = new DTDParser("test.xml");
		mJavaGenerator = new JavaGenerator();
		parser.generateJavaModel();
		parser.getJm().generate(mJavaGenerator);
		return mJavaGenerator.getValue().toString();
	}

	public String generateDotFromFileXML() {
		DTDParser parser = new DTDParser("test.xml");
		mJavaGenerator = new JavaGenerator();
		parser.generateJavaModel();
		JavaGraphvizTransformer transformer = new JavaGraphvizTransformer();
		transformer.generate(parser.getJm());
		GraphvizGraphBuilder graphBuilder = new GraphvizGraphBuilder(
				"testGraph", EGraphType.UNDIRECTED);
		graphBuilder.addAllStatementToGraph(transformer.getStatements());
		DotGenerator dotGenerator = new DotGenerator();
		graphBuilder.getGraph().generate(dotGenerator);
		return dotGenerator.getValue().toString();
	}

	public Group generateVisuFromFileXML() {
		DTDParser parser = new DTDParser("test.xml");
		mJavaGenerator = new JavaGenerator();
		parser.generateJavaModel();
		JavaGraphvizTransformer transformer = new JavaGraphvizTransformer();
		transformer.generate(parser.getJm());
		GraphvizGraphBuilder graphBuilder = new GraphvizGraphBuilder(
				"testGraph", EGraphType.UNDIRECTED);
		graphBuilder.addAllStatementToGraph(transformer.getStatements());
		DotGenerator dotGenerator = new DotGenerator();
		graphBuilder.getGraph().generate(dotGenerator);
		DotParser dotParser = new DotParser(dotGenerator.getValue().toString());
		dotParser.parse();
		return dotParser.getGraph().paintGraph();
	}

	public String generateDotFromUSpeed(String string) {
		mVisiteur = new Visiteur();
		USpeedParser u = new USpeedParser();
		u.generateSchemaUml(string);
		u.getsU().accept(mVisiteur);
		mVisiteur.generateXML("schema.xml");
		DTDParser parser = new DTDParser("schema.xml");
		parser.generateJavaModel();
		JavaGraphvizTransformer transformer = new JavaGraphvizTransformer();
		transformer.generate(parser.getJm());
		GraphvizGraphBuilder graphBuilder = new GraphvizGraphBuilder(
				"testGraph", EGraphType.UNDIRECTED);
		graphBuilder.addAllStatementToGraph(transformer.getStatements());
		DotGenerator dotGenerator = new DotGenerator();
		graphBuilder.getGraph().generate(dotGenerator);
		return dotGenerator.getValue().toString();
	}

	public Group generateVisuFromUSpeed(String string) {
		mVisiteur = new Visiteur();
		USpeedParser u = new USpeedParser();
		u.generateSchemaUml(string);
		u.getsU().accept(mVisiteur);
		mVisiteur.generateXML("schema.xml");
		DTDParser parser = new DTDParser("schema.xml");
		parser.generateJavaModel();
		JavaGraphvizTransformer transformer = new JavaGraphvizTransformer();
		transformer.generate(parser.getJm());
		GraphvizGraphBuilder graphBuilder = new GraphvizGraphBuilder(
				"testGraph", EGraphType.UNDIRECTED);
		graphBuilder.addAllStatementToGraph(transformer.getStatements());
		DotGenerator dotGenerator = new DotGenerator();
		graphBuilder.getGraph().generate(dotGenerator);
		DotParser dotParser = new DotParser(dotGenerator.getValue().toString());
		dotParser.parse();
		return dotParser.getGraph().paintGraph();
	}
}
